package com.xx.happychatboot.middleware.rocketmq.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.xx.happychatboot.middleware.rocketmq.domain.entity.SecureInvokeRecord;
import com.xx.happychatboot.middleware.rocketmq.mapper.SecureInvokeRecordMapper;
import com.xx.happychatboot.middleware.rocketmq.service.SecureInvokeService;
import io.mybatis.service.AbstractService;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 本地消息表dao层
 */
@Component
public class SecureInvokeRecordDao extends AbstractService<SecureInvokeRecord, Long, SecureInvokeRecordMapper> {

    /**
     * 获取需要重试的记录
     * @return 需要重试的 SecureInvokeRecord 列表
     */
    public List<SecureInvokeRecord> getWaitRetryRecords() {
        Date now = new Date();
        //查2分钟前的失败数据。避免刚入库的数据被查出来
        DateTime afterTime = DateUtil.offsetMinute(now, (int) SecureInvokeService.RETRY_INTERVAL_MINUTES);
        return baseMapper.wrapper()
                .eq(SecureInvokeRecord::getStatus, SecureInvokeRecord.STATUS_WAIT)
                .lt(SecureInvokeRecord::getNextRetryTime, new Date())
                .lt(SecureInvokeRecord::getCreateTime, afterTime)
                .list();
    }
}
